<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="医院名称" prop="hospitalName">
        <el-input v-model="queryParams.hospitalName" placeholder="请输入医院名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="科室" prop="depart">
        <el-select v-model="queryParams.depart" placeholder="请选择科室">
          <el-option v-for="dict in departList" :key="dict.id" :label="dict.depart" :value="dict.depart" />
        </el-select>
      </el-form-item>
      <el-form-item label="联系人" prop="relationName">
        <el-input v-model="queryParams.relationName" placeholder="请输入联系人" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系电话" prop="relationPhone">
        <el-input v-model="queryParams.relationPhone" placeholder="请输入联系电话" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['manager:hospital:add']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['manager:hospital:edit']" type="success" icon="el-icon-edit" size="mini" :disabled="single"
          @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['manager:hospital:remove']" type="danger" icon="el-icon-delete" size="mini" :disabled="multiple"
          @click="handleDelete">删除</el-button>
      </el-col>

      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['manager:hospital:export']"-->
      <!--          type="warning"-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="hospitalList" @selection-change="handleSelectionChange" height="68vh">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="医院标识" align="center" prop="hospitalID" />
      <el-table-column label="医院名称" align="center" prop="hospitalName" />
      <el-table-column label="科室" align="center" prop="depart" />
      <el-table-column label="会诊账号" align="center" prop="treatAccount" />
      <el-table-column label="联系人" align="center" prop="relationName" />
      <el-table-column label="联系电话" align="center" prop="relationPhone" />
      <el-table-column label="类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['manager:hospital:edit']" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button v-hasPermi="['manager:hospital:remove']" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-lightning" style="color: #ff0000" @click="handelLight(scope.row)" v-show="scope.row.status == 1">
            停用
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改外援对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型" @change="handelChange">
            <el-option v-for="dict in depOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="医院名称" prop="hospitalName" v-show="hosptialShowToggle">
          <el-input v-model="form.hospitalName" placeholder="请输入医院名称" />
        </el-form-item>
        <el-form-item label="科室" prop="depart">
          <!-- <el-input v-model="form.depart" placeholder="请输入科室" /> -->
          <el-select v-model="form.depart" placeholder="请选择科室">
            <el-option v-for="depart in departList" :key="depart.id" :label="depart.depart" :value="depart.depart" />
          </el-select>
        </el-form-item>
        <el-form-item label="会诊账号" prop="treatAccount">
          <el-input v-model="form.treatAccount" placeholder="请输入会诊账号" />
        </el-form-item>
        <el-form-item label="联系人" prop="relationName">
          <el-input v-model="form.relationName" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="relationPhone">
          <el-input v-model="form.relationPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="密码" prop="passwd">
          <el-input v-model="form.passwd" placeholder="请输入密码,为空缺省123456" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listHospital,
    getHospital,
    delHospital,
    addHospital,
    updateHospital,
    exportHospital
  } from "@/api/biz/hospital";
  import {
    listDepart
  } from "@/api/biz/depart";
  export default {
    name: "Hospital",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        hosptialShowToggle: true,
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 外援表格数据
        hospitalList: [],
        departList: [],
        // 弹出层标题
        title: "",
        // 类型选择
        depOptions: [],
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          hospitalName: undefined,
          depart: undefined,
          relationName: undefined,
          relationPhone: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          type: [{
            required: true,
            message: "类型能为空",
            trigger: "blur"
          }],
          // hospitalName: [
          //   { required: true, message: '医院名称不能为空', trigger: 'blur' }
          // ],
          // depart: [
          //   { required: true, message: '科室不能为空', trigger: 'blur' }
          // ],
          treatAccount: [{
            required: true,
            message: "会诊账号不能为空",
            trigger: "blur"
          }],
          relationName: [{
            required: true,
            message: "联系人不能为空",
            trigger: "blur"
          }],
          relationPhone: [{
            required: true,
            message: "联系电话不能为空",
            trigger: "blur"
          }]
        }
      };
    },
    created() {
      this.getList();
      this.getDepartLists();
      this.getDicts("hospital_dep_status").then(response => {
        this.depOptions = response.data;
      });
    },
    methods: {
      typeFormat(row, column) {
        if (row.type == 1) {
          return "医院";
        } else {
          return "科室";
        }
        // return this.selectDictLabel(this.statusOptions, row.status);
      },
      /** 查询外援列表 */
      getList() {
        this.loading = true;
        listHospital(this.queryParams).then(response => {
          this.hospitalList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 获取科室列表
      getDepartLists() {
        listDepart().then(response => {
          this.departList = response.rows;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          hospitalID: undefined,
          hospitalName: undefined,
          depart: undefined,
          treatAccount: undefined,
          relationName: undefined,
          relationPhone: undefined,
          uid: undefined,
          passwd: undefined,
          type: undefined
        };
        this.resetForm("form");
      },
      // type选项改变
      handelChange() {
        this.hosptialShowToggle = this.form.type == 2 ? false : true;
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.hospitalID);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加外援";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const hospitalID = row.hospitalID || this.ids;
        getHospital(hospitalID).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改外援";
        });
      },
    /**  冻结操作 */
    handelLight(row) {
      const hospitalID = row.hospitalID || this.ids;
        this.$confirm( '是否确认停用编号为"' + hospitalID + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.form.hospitalID = hospitalID
          this.form.status = 2
          updateHospital(this.form).then(response => {
            if (response.code === 200) {
              this.$message({
                type: 'success',
                message: '停用成功!'
              });
              this.open = false;
              this.getList();
            } else {
              this.msgError(response.msg);
            }
          });

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.hospitalID !== undefined) {
              updateHospital(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addHospital(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const hospitalIDs = row.hospitalID || this.ids;
        this.$confirm(
            '是否确认删除外援编号为"' + hospitalIDs + '"的数据项?',
            "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }
          )
          .then(function() {
            return delHospital(hospitalIDs);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          })
          .catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm("是否确认导出所有外援数据项?", "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(function() {
            return exportHospital(queryParams);
          })
          .then(response => {
            this.download(response.msg);
          })
          .catch(function() {});
      }
    }
  };
</script>
<style scoped="scoped">
  /* 分页 */
  .pagination-container {
    height: 80px;
    margin-top: 0px;
    padding: 23px 20px !important;
  }

  /* 分页  end */
</style>
